﻿@model InspectionListModel
@using Nop.Core.Domain.XJ
@using Nop.Services.Security
@{
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    var permissionService = EngineContext.Current.Resolve<IPermissionService>();
    var canManageMaintenancesView = permissionService.Authorize(StandardPermissionProvider.ManageMaintenancesView);
    var canManageMyMaintenances = permissionService.Authorize(StandardPermissionProvider.ManageMyMaintenances);
    var canManageInspectionPlansView = permissionService.Authorize(StandardPermissionProvider.ManageInspectionPlansView);

    //page title
    ViewBag.Title = "巡检记录列表";
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("Inspection list");
}

@Html.AntiForgeryToken()
<div class="content-header clearfix">
    <h1 class="pull-left">
        巡检记录列表
    </h1>
    <div class="pull-right">
        @*<a href="@Url.Action("Create")" class="btn bg-blue">
                <i class="fa fa-plus-square"></i>
                @T("Admin.Common.AddNew")
            </a>*@
    </div>
</div>
<div class="content">
    <div class="form-horizontal">
        <div class="panel-group">
            <div class="panel panel-default panel-search">
                <div class="panel-body">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                <div class="col-md-2 col-search">
                                    <div class="label-wrapper"><label class="control-label" for="SearchTypeId" title="">巡检类型</label></div>
                                    <div class="col-content">
                                        @Html.NopDropDownListFor(model => model.SearchTypeId, Model.AvailableTypes)
                                    </div>
                                </div>
                                <div class="col-md-2 col-search">
                                    <div class="label-wrapper"><label class="control-label" for="SearchPNo" title="">计划编号</label></div>
                                    <div class="col-content">
                                        @Html.NopEditorFor(model => model.SearchPNo)
                                    </div>
                                </div>
                                <div class="col-md-2 col-search">
                                    <div class="label-wrapper"><label class="control-label" for="SearchCategoryId" title="">设备分类</label></div>
                                    <div class="col-content">
                                        @Html.NopDropDownListFor(model => model.SearchCategoryId, Model.AvailableCategorys)
                                    </div>
                                </div>
                                <div class="col-md-2 col-search">
                                    <div class="label-wrapper"><label class="control-label" for="SearchPlaceId" title="">巡检点</label></div>
                                    <div class="col-content">
                                        @Html.NopDropDownListFor(model => model.SearchPlaceId, Model.AvailablePlaces)
                                    </div>
                                </div>
                                <div class="col-md-2 col-search">
                                    <div class="label-wrapper" style="flex:2;"><label class="control-label" for="SearchTeamId" title="">巡检班组</label></div>
                                    <div class="col-flex4">
                                        @Html.EditorFor(model => model.SearchTeamId, new { SelectList = Model.AvailableDepartments })
                                    </div>
                                </div>
                                <div class="col-md-2 col-search">
                                    <div class="label-wrapper"><label class="control-label" for="SearchPersonId" title="">巡检人</label></div>
                                    <div class="col-content">
                                        @Html.NopDropDownListFor(model => model.SearchPersonId, Model.AvailableUsers)
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                <div class="col-md-10">
                                </div>
                                <div class="col-md-2" style="text-align:right;">
                                    <button type="button" id="search-categories" class="btn btn-primary"><i class="fa fa-search"></i>@T("Admin.Common.Search")</button>
                                    <button type="button" id="cancel-categories" class="btn btn-default"><i class="fa fa-refresh"></i>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="panel panel-default">
                    <div class="panel-body">
                        <div id="organizations-grid"></div>

                        <script>
                        $(document).ready(function() {
                            $("#organizations-grid").kendoGrid({
                                dataSource: {
                                    type: "json",
                                    transport: {
                                        read: {
                                            url: "@Html.Raw(Url.Action("List", "Inspection"))",
                                            type: "POST",
                                            dataType: "json",
                                            data: additionalData
                                        }
                                    },
                                    schema: {
                                        data: "Data",
                                        total: "Total",
                                        errors: "Errors"
                                    },
                                    error: function(e) {
                                        display_kendoui_grid_error(e);
                                        // Cancel the changes
                                        this.cancelChanges();
                                    },
                                    pageSize: @(defaultGridPageSize),
                                    serverPaging: true,
                                    serverFiltering: true,
                                    serverSorting: true
                                },
                                pageable: {
                                    refresh: true,
                                    pageSizes: [@(gridPageSizes)],
                                    @Html.Partial("_GridPagerMessages")
                                },
                                editable: {
                                    confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                    mode: "inline"
                                },
                                scrollable: false,
                                columns: [{
                                    field: "TypeName",
                                    title: "类型",
                                    width: 50,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                },{
                                    field: "InspectionPlanId",
                                    title: "计划编号",
                                    width: 80,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                    template: '#if(@(canManageInspectionPlansView?1:0)== 1&&InspectionPlanId>0){# <a target="_blank" href="@Url.Content("~/Admin/InspectionPlan/View/")#=InspectionPlanId#">#=PNo#</a> #}else if(InspectionPlanId>0){# #=PNo# #}#'
                                },{
                                    field: "MaintenanceId",
                                    title: "维修编号",
                                    width: 80,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                    template: '#if((@(canManageMaintenancesView ? 1:0)== 1||@(canManageMyMaintenances ? 1:0)== 1)&&MaintenanceId>0){# <a target="_blank" href="@Url.Content("~/Admin/Maintenance/View/")#=MaintenanceId#">#=XNo#</a> #}else if(MaintenanceId>0){# #=XNo# #}#'
                                },{
                                    field: "Name",
                                    title: "计划名称",
                                    width: 200,
                                    headerAttributes: { style: "text-align:center" },
                                },{
                                    field: "Comment",
                                    title: "巡检说明",
                                    width: 200,
                                    headerAttributes: { style: "text-align:center" },
                                },{
                                    field: "TeamName",
                                    title: "巡检班组",
                                    width: 80,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                 },{
                                    field: "CustomerName",
                                    title: "巡检人员",
                                    width: 80,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                },{
                                    field: "CreatedOn",
                                    title: "巡检时间",
                                    width: 100,
                                    type: "date",
                                    format: "{0:yyyy/MM/dd}",
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                },{
                                    field: "Id",
                                    title: "@T("Admin.Common.Edit")",
                                    width: 50,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                    template: '<i class="fa fa-eye fa-active" title="查看" onclick="javascript:window.location.href=\'View/#=Id#\';"></i>',
                                }]
                            });
                        });
                        $("#@Html.FieldIdFor(model => model.SearchTeamId)").change(function () {
                            var selectedItem = $(this).val();
                            var ddlStates = $("#@Html.FieldIdFor(model => model.SearchPersonId)");
                            $.ajax({
                                cache: false,
                                type: "GET",
                                url: "@(Url.Action("GetUsersByDepartmentId", "Customer"))",
                                data: { "departId": selectedItem },
                                success: function(data) {
                                    ddlStates.html('');
                                    $.each(data, function(id, option) {
                                        ddlStates.append($('<option></option>').val(option.id).html(option.name));
                                    });
                                },
                                error: function(xhr, ajaxOptions, thrownError) {
                                    alert('Failed to retrieve states.');
                                }
                            });
                        });
                        </script>
                        <script type="text/javascript">
                        $(document).ready(function() {
                            //search button
                            $('#search-categories').click(function() {
                                //search
                                var grid = $('#organizations-grid').data('kendoGrid');
                                grid.dataSource.page(1); //new search. Set page size to 1
                                //grid.dataSource.read(); we already loaded the grid above using "page" function
                                return false;
                            });
                            //cancel button
                            $('#cancel-categories').click(function () {
                                //cancel
                                var grid = $('#organizations-grid').data('kendoGrid');
                                $('#@Html.FieldIdFor(model => model.SearchCategoryId)').val('0');
                                $('#@Html.FieldIdFor(model => model.SearchPlaceId)').val('0');
                                $('#@Html.FieldIdFor(model => model.SearchTypeId)').val('0');
                                $('#@Html.FieldIdFor(model => model.SearchTeamId)').val('0');
                                $('#@Html.FieldIdFor(model => model.SearchPersonId)').val('0');
                                $('#@Html.FieldIdFor(model => model.SearchPNo)').val('');
                                $('#@Html.FieldIdFor(model => model.SearchName)').val('');
                                $("#select_menuSearchTeamId").attr("value", "全部");
                                grid.dataSource.page(1); //new search. Set page size to 1
                                //grid.dataSource.read(); we already loaded the grid above using "page" function
                                return false;
                            });

                            $("#@Html.FieldIdFor(model => model.SearchPNo)").keydown(function(event) {
                                if (event.keyCode == 13) {
                                    $("#search-categories").click();
                                    return false;
                                }
                            });

                            $("#@Html.FieldIdFor(model => model.SearchName)").keydown(function(event) {
                                if (event.keyCode == 13) {
                                    $("#search-categories").click();
                                    return false;
                                }
                            });
                        });

                        function additionalData() {
                            var data = {
                                SearchCategoryId: $('#@Html.FieldIdFor(model => model.SearchCategoryId)').val(),
                                SearchPlaceId: $('#@Html.FieldIdFor(model => model.SearchPlaceId)').val(),
                                SearchTypeId: $('#@Html.FieldIdFor(model => model.SearchTypeId)').val(),
                                SearchTeamId: $('#@Html.FieldIdFor(model => model.SearchTeamId)').val(),
                                SearchPersonId: $('#@Html.FieldIdFor(model => model.SearchPersonId)').val(),
                                SearchPNo: $('#@Html.FieldIdFor(model => model.SearchPNo)').val(),
                                SearchName: $('#@Html.FieldIdFor(model => model.SearchName)').val()
                            };
                            addAntiForgeryToken(data);
                            return data;
                        }
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>